#! /bin/bash
#   
# redis - this script starts and stops the redis-server daemon
#   
# chkconfig:    2345 80 90
# description:  Redis is a persistent key-value database
#   
### BEGIN INIT INFO
# Provides:          redis
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    redis-server daemon
# Description:        redis-server daemon
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
   
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/etc/redis.conf"
   
case "$1" in
    start)
        if [ -f $PIDFILE ]; then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo -n "Starting Redis server..."
                $EXEC $CONF
                if [ "$?"="0" ]; then
                    echo " done"
                else
                    echo " failed"
                fi
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]; then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping  Redis server..."
                $REDIS_CLI -p $REDISPORT shutdown
                if [ "$?"="0" ]; then
                    echo " done"
                else
                    echo " failed"
                fi
        fi
        ;;
   restart)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart}" >&2
        exit 1
esac